package com.dlxj.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.dlxj.entity.Group;
import com.dlxj.entity.Model;
import com.dlxj.entity.User;
import com.dlxj.entity.UserGroup;
import com.dlxj.service.GroupService;
import com.dlxj.service.ModelService;
import com.dlxj.service.PurviewService;
import com.dlxj.util.MyJSON;

public class GroupAction {

	GroupService groupService;
	PurviewService purviewService;
	ModelService modelService;
	String json;
	Group group;
	String name;
	String mid;
	int gid;

	public String findGroupList() {

		List<Group> groups = groupService.findGroupList();
		json = MyJSON.toJson(groups, "group", "users", "groups").toString();
		return "success";
	}

	public String deleteGroup() {

		int code = groupService.deleteGroup(gid);
		json = "{code:" + code + "}";
		return "success";
	}

	public String updateGroupName() {
		int code = groupService.modifyGroupName(gid, name);
		json = "{code:" + code + "}";
		return "success";
	}

	public String addGroup() {
		List<Model> models = new ArrayList<Model>();
		String[] mids = mid.split(",");
		for (String string : mids) {
			models.add(modelService.findModelById(Integer.parseInt(string)));
		}
		int pid = purviewService.addPurview(models);
		if (pid != -1) {
			group = new Group();
			group.setCreateTime(new Date());
			group.setName(name);
			group.setState(1);
			group.setPurview(purviewService.findPurviewById(pid));
			group.set_user((User) ServletActionContext.getRequest()
					.getSession().getAttribute("user"));
			int code = groupService.addGroup(group);
			json = "{code:" + code + "}";
		} else {
			json = "{code:" + pid + "}";
		}
		return "success";
	}

	public String testHighcharts() {

		List<UserGroup> resultList = new ArrayList<UserGroup>();

		List<Object[]> code = (List<Object[]>) groupService.bing();
		for (Object[] objects : code) {
			resultList.add(new UserGroup(objects[0] + "", Integer
					.parseInt(objects[1] + "")));
		}
		json = MyJSON.toJson(resultList).toString();
		/*
		 * List<BrowserShare> resultList = new ArrayList<BrowserShare>();
		 * resultList.add(new BrowserShare("Chrome",18.55F)); resultList.add(new
		 * BrowserShare("Firefoc",19.99F)); resultList.add(new
		 * BrowserShare("IE",54.13F)); resultList.add(new
		 * BrowserShare("Oher",0.49F)); resultList.add(new
		 * BrowserShare("Oprea",1.63F)); resultList.add(new
		 * BrowserShare("Safari",5.21F)); json =
		 * MyJSON.toJson(resultList).toString();
		 */
		return "success";
	}

	public GroupService getGroupService() {
		return groupService;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public int getGid() {
		return gid;
	}

	public void setGid(int gid) {
		this.gid = gid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public PurviewService getPurviewService() {
		return purviewService;
	}

	public void setPurviewService(PurviewService purviewService) {
		this.purviewService = purviewService;
	}

	public String getMid() {
		return mid;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}

	public ModelService getModelService() {
		return modelService;
	}

	public void setModelService(ModelService modelService) {
		this.modelService = modelService;
	}

}
